/*=============================================================================================================
 * ModifyFragment.java
 *
 * 1.프로젝트 : ToYou - 명함관리 어플
 * 2.패 키 지 : com.trillion.toyou
 * 3.작업환경 : Android
 * 2.작 업 자 : 
 * 3.작업일자 : 2014.12.09
 * 4.내    용 : 명함내용을 수정하는 프래그먼트 화면
 * 5.수 정 자 : -----
 * 6.수정일자 : -----
 * 7.수정내용 : -----
 * =============================================================================================================*/
package com.trillion.toyou;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ModifyFragment extends Fragment implements OnClickListener{
View view = null;
	
	ImageButton bt_input = null;
	ImageButton bt_cancel = null;
	ImageButton bt_back = null;
	
	TextView nname = null;
	TextView grade = null;
	TextView company = null;
	TextView email = null; 
	TextView telNum = null;
	
	TextView phoneNum = null;
	TextView address = null;
	TextView department= null;
	LinearLayout topTitle = null;
	
	
	int style_card = 0;
	
	Fragment frag = null;
	FragmentTransaction trans = null;
	Bundle bundle = null;
	String nid = "";
	
	DBHelper db = null;
	Data data = null; 
	
	boolean check_modify = false;
	
	InputMethodManager imm = null;
			
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.view = inflater.inflate(R.layout.input_layout, container, false);
		
		MainActivity.page_check = new MainActivity().BACK_OK;
		
		nname = (TextView)view.findViewById(R.id.input_layout_et_nname);
		grade = (TextView)view.findViewById(R.id.input_layout_et_grade);
		company = (TextView)view.findViewById(R.id.input_layout_et_company);
		email = (TextView)view.findViewById(R.id.input_layout_et_email);
		telNum = (TextView)view.findViewById(R.id.input_layout_et_telNum);

		phoneNum = (TextView)view.findViewById(R.id.input_layout_et_phoneNum);
		address = (TextView)view.findViewById(R.id.input_layout_et_address);
		department= (TextView)view.findViewById(R.id.input_layout_et_department);
		
		bt_input = (ImageButton)view.findViewById(R.id.input_layout_bt_input);
		bt_cancel = (ImageButton)view.findViewById(R.id.input_layout_bt_cancel);
		bt_back = (ImageButton)view.findViewById(R.id.input_layout_bt_back2);
		
		imm = (InputMethodManager)getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);		
		
		
		
		
		this.topTitle = (LinearLayout)this.view.findViewById(R.id.input_layout_top);
		
		

		
		
		
		
		
		bt_back.setOnClickListener(this);
		bt_input.setOnClickListener(this);
		bt_cancel.setOnClickListener(this);
		

		
		bundle = getArguments();
		
		db = new DBHelper(getActivity(), "toyou", null, 1);
		data = new Data();
		
		
		nid = bundle.getString("id"); 
		data = db.selectone(nid);
		
		mycard_load(bundle);
		
		return view;
	}
	
	
	@Override
		public void onResume() {
			new NfcOnOffChange(this.topTitle, R.drawable.top_bar_on_nfc_my, R.drawable.top_bar_off_nfc_my);
			super.onResume();
		}
	
	
	
	
	
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		Fragment frag = null;
		FragmentTransaction trans = null;
		int a = data.getNid();

		
		switch(id)
		{
		case R.id.input_layout_bt_input :
			inputinput();
			if(check_modify)
			{
			bundle.putInt("id", a);
			trans = getFragmentManager().beginTransaction();
			frag = new MyViewFragment();
			frag.setArguments(this.bundle);
			trans.replace(R.id.root_layout, frag);
			trans.addToBackStack(null);
			trans.commitAllowingStateLoss();
			}
			
			break;
		case R.id.input_layout_bt_cancel :
			bundle.putInt("id", a);
			trans = getFragmentManager().beginTransaction();
			frag = new MyViewFragment();
			frag.setArguments(this.bundle);
			trans.replace(R.id.root_layout, frag);
			trans.addToBackStack(null);
			trans.commitAllowingStateLoss();
			break;
		case R.id.input_layout_bt_back2:

			imm.hideSoftInputFromWindow(company.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(nname.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(grade.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(department.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(address.getWindowToken(), 0);

			imm.hideSoftInputFromWindow(telNum.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(phoneNum.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(email.getWindowToken(), 0);
			
			
			bundle.putInt("id", a);
			trans = getFragmentManager().beginTransaction();
			frag = new MyViewFragment();
			frag.setArguments(this.bundle);
			trans.replace(R.id.root_layout, frag);
			trans.addToBackStack(null);
			trans.commitAllowingStateLoss();
			break;
		}

		
	}
	public void mycard_load(Bundle bundle)
	{		
		nname.setText(data.getNname());
		grade.setText(data.getGrade());
		company.setText(data.getCompany());
		email.setText(data.getEmail());
		telNum.setText(data.getTelNum());
		
		phoneNum.setText(data.getPhoneNum());
		address.setText(data.getAddress());
		department.setText(data.getDepartment());
	}
	
	
	public void inputinput()
	{
		check_modify = false;
		
		db = new DBHelper(getActivity(), "toyou", null, 1);
		data = new Data();
		
		nid = bundle.getString("id"); 
		data = db.selectone(nid);
		
		if(company.getText().toString().equals(""))
		{
			data.setCompany(company.getText().toString());
			company.requestFocus();
			imm.showSoftInput(company, InputMethodManager.SHOW_FORCED);
			Toast.makeText(getActivity(), "회사를 입력해주세요.", Toast.LENGTH_SHORT).show();
			return;
			
		}
		else if(grade.getText().toString().equals(""))
		{
			data.setGrade(grade.getText().toString());
			grade.requestFocus();
			imm.showSoftInput(grade, InputMethodManager.SHOW_FORCED);
			Toast.makeText(getActivity(), "직급을 입력해주세요.", Toast.LENGTH_SHORT).show();
			return;
		}
		else if(department.getText().toString().equals(""))
		{
			data.setDepartment(department.getText().toString());
			department.requestFocus();
			imm.showSoftInput(department, InputMethodManager.SHOW_FORCED);
			Toast.makeText(getActivity(), "부서를 입력해주세요.", Toast.LENGTH_SHORT).show();
			return;
		}
		else if(nname.getText().toString().equals(""))
		{
			data.setNname(nname.getText().toString());
			nname.requestFocus();
			imm.showSoftInput(nname, InputMethodManager.SHOW_FORCED);
			Toast.makeText(getActivity(), "이름을 입력해주세요.", Toast.LENGTH_SHORT).show();
			return;
		}
		else if(address.getText().toString().equals(""))
		{
			data.setAddress(address.getText().toString());
			address.requestFocus();
			imm.showSoftInput(address, InputMethodManager.SHOW_FORCED);
			Toast.makeText(getActivity(), "주소를 입력해주세요.", Toast.LENGTH_SHORT).show();
			return;
		}
		else if(telNum.getText().toString().equals(""))
		{
			data.setTelNum(telNum.getText().toString());
			telNum.requestFocus();
			imm.showSoftInput(telNum, InputMethodManager.SHOW_FORCED);
			Toast.makeText(getActivity(), "회사번호를 입력해주세요.", Toast.LENGTH_SHORT).show();
			return;
		}
		else if(phoneNum.getText().toString().equals(""))
		{
			data.setPhoneNum(phoneNum.getText().toString());
			phoneNum.requestFocus();
			imm.showSoftInput(phoneNum, InputMethodManager.SHOW_FORCED);
			Toast.makeText(getActivity(), "핸드폰번호를 입력해주세요.", Toast.LENGTH_SHORT).show();
			return;
		}
		else if(email.getText().toString().equals(""))
		{
			data.setEmail(email.getText().toString());
			email.requestFocus();
			imm.showSoftInput(email, InputMethodManager.SHOW_FORCED);
			Toast.makeText(getActivity(), "이메일을 입력해주세요.", Toast.LENGTH_SHORT).show();
			return;
		}
		else{
			data.setNname(nname.getText().toString());
			data.setGrade(grade.getText().toString());
			data.setCompany(company.getText().toString());
			data.setEmail(email.getText().toString());
			data.setTelNum(telNum.getText().toString());
			
			data.setPhoneNum(phoneNum.getText().toString());
			data.setAddress(address.getText().toString());
			data.setDepartment(department.getText().toString());
		
			db.modify(data);
			check_modify = true;
			Toast.makeText(getActivity(), "수정 성공", Toast.LENGTH_SHORT).show();
				
		}
	
	}

}
